<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="序号" prop="serial">
        <el-input
          v-model="queryParams.serial"
          placeholder="请输入序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="新闻标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入新闻标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻内容" prop="substance">
        <el-input
          v-model="queryParams.substance"
          placeholder="请输入新闻内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片" prop="pic">
        <el-input
          v-model="queryParams.pic"
          placeholder="请输入图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链接" prop="outerInterlinkage">
        <el-input
          v-model="queryParams.outerInterlinkage"
          placeholder="请输入链接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻类别编号" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入新闻类别编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="新闻作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入新闻作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['committee:journalism:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['committee:journalism:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['committee:journalism:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['committee:journalism:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="journalismList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="新闻标题" align="center" prop="title" />
      <el-table-column label="图片" align="center" prop="pic" >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.pic"
            style="width: 100px; height: 100px"
            :src="scope.row.pic"
            :preview-src-list="[scope.row.pic]"
            fit="cover"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="链接" align="center" prop="outerInterlinkage" />
      <el-table-column label="排序" align="center" prop="sort" />
      <!-- <el-table-column label="内容" align="center" prop="substance" />
      <el-table-column label="位置" align="center" prop="location" />
      <el-table-column label="新闻类别编号" align="center" prop="categoryId" />
      <el-table-column label="新闻作者" align="center" prop="author" /> -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['committee:journalism:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['committee:journalism:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改新闻对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="序号" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入序号" />
        </el-form-item> -->
        <el-form-item label="新闻标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入新闻标题" />
        </el-form-item>
        <el-form-item label="图片" prop="pic">
            <el-upload
            style="width: 80%"
            :action="uploadFileUrl"
            :limit="1"
            list-type="picture"
            :on-change="changePicImage"
            :on-remove="removePicImage"
            :on-error="handleUploadError"
            :on-success="handleUploadSuccess"
            :file-list="picFileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="链接" prop="outerInterlinkage">
          <el-input v-model="form.outerInterlinkage" placeholder="请输入链接" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="内容" prop="substance">
          <el-input v-model="form.substance" type="textarea" :autosize="{ minRows: 4, maxRows: 8}" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看新闻详情 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="550px" append-to-body>
      <el-form ref="form" :model="form" label-width="60px">
        <!-- <el-form-item label="序号" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入序号" />
        </el-form-item> -->
        <el-form-item label="标题" prop="title">
          {{form.title}}
        </el-form-item>
        <el-form-item label="图片" prop="pic">
            <el-image
            v-if="form.pic"
            style="width: 100px; height: 100px"
            :src="form.pic"
            :preview-src-list="[form.pic]"
            fit="cover"
          >
          </el-image>
        </el-form-item>
        <el-form-item label="链接" prop="outerInterlinkage">
          {{form.outerInterlinkage}}
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          {{form.sort}}
        </el-form-item>
        <el-form-item label="内容" prop="substance">
          <div style="width: 90%">{{form.substance}}</div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listJournalism, getJournalism, delJournalism, addJournalism, updateJournalism } from "@/api/journalism";

export default {
  name: "Journalism",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻表格数据
      journalismList: [],
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      openDetail:false,
      // 图片
      picFileList: [],
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/api/images/uploadLocal',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId:1,
        serial: null,
        title: null,
        sort: null,
        pic: null,
        outerInterlinkage: null,
        substance: null,
        location: null,
        author: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: '新闻标题不能为空', trigger: 'blur' }],
        pic: [{ required: true, message: '图片不能为空', trigger: 'blur' }],
        // outerInterlinkage: [{ required: true, message: '链接不能为空', trigger: 'blur' }],
        sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        substance: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询新闻列表 */
    getList() {
      this.loading = true;
      listJournalism(this.queryParams).then(response => {
        this.journalismList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serial: null,
        id: null,
        title: null,
        sort: null,
        pic: null,
        outerInterlinkage: null,
        substance: null,
        location: null,
        author: null,
        createTime: null
      };
      this.resetForm("form");
      this.picFileList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加新闻";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getJournalism(id).then(response => {
        this.form = response.data;
        this.openDetail = true;
        this.title = "查看新闻详情";
      });
    },

    changePicImage(file, fileList) {
      console.log(file.code);
      if (file.status === 'success') {
        this.form.pic = file.response.data.url;
        this.picFileList = fileList;
      }
    },
    removePicImage() {
      this.form.pic = null;
      this.picFileList = [];
    },
    handleUploadError() {
      this.$modal.msgError('上传失败');
    },
    handleUploadSuccess(res) {
      console.log(res, 'resssss');
      if (res.code == 200) {
        this.$modal.msgSuccess('上传成功');
      } else {
        this.$modal.msgError('上传失败');
      }
    },

    /** 提交按钮 */
    submitForm() {
        this.form.categoryId=1;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateJournalism(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJournalism(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除新闻编号为"' + ids + '"的数据项？').then(function() {
        return delJournalism(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('committee/journalism/export', {
        ...this.queryParams
      }, `journalism_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
